package rest

import (
	"fmt"

	"nh-poc/pkg/common"

	"github.com/go-resty/resty/v2" // 添加Resty依赖
)

type RestClient struct {
	client *resty.Client
}

func NewRestClient() *RestClient {
	return &RestClient{
		client: resty.New().SetTimeout(common.DefaultTimeout),
	}
}

func (c *RestClient) ReportEvent(reportUrl string, event *common.HKEvent) (err error) {
	request := &common.ParkResponse{
		Code:    200,
		Message: "ok",
		Data:    event,
	}

	resp, err := c.client.R().
		SetBody(request).                              // 设置请求体
		SetHeader("Content-Type", "application/json"). // 新增请求头设置
		SetHeader("Accept", "application/json").       // 新增接受头设置
		Post(reportUrl)                                // 指定POST路径

	if err != nil {
		return
	}

	// 检查响应状态码
	if resp.StatusCode() != 200 {
		err = fmt.Errorf("unexpected status code: %d", resp.StatusCode())
		return
	}

	return
}
